package user.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import user.dao.SystemAdminDao;
import user.model.User;
import user.util.StringUtil;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserAddDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField userId;
	private JTextField UserName;
	private JTextField password;
	private SystemAdminDao systemAdminDao = new SystemAdminDao();
	private UserManagerFrame userManagerFrame = new UserManagerFrame();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UserAddDialog dialog = new UserAddDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UserAddDialog() {
		setTitle("用户添加");
		setBounds(100, 100, 496, 540);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		JLabel lblNewLabel = new JLabel("添加新用户");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 22));
		JLabel lblNewLabel_1 = new JLabel("用户账号");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 18));
		JLabel lblNewLabel_2 = new JLabel("用户名");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 18));
		JLabel lblNewLabel_3 = new JLabel("密码");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 18));
		
		userId = new JTextField();
		userId.setColumns(10);
		
		UserName = new JTextField();
		UserName.setColumns(10);
		
		password = new JTextField();
		password.setColumns(10);
		
		JButton btnNewButton = new JButton("重置");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset();
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JButton btnNewButton_1 = new JButton("添加");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UserAddActionPerformed(e);                        
			}
		});
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JButton btnNewButton_2 = new JButton("返回");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnNewButton_2.setFont(new Font("宋体", Font.PLAIN, 20));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(175)
							.addComponent(lblNewLabel))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(90)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_1)
								.addComponent(lblNewLabel_2)
								.addComponent(lblNewLabel_3))
							.addGap(18)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
								.addComponent(password)
								.addComponent(UserName)
								.addComponent(userId, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(58)
							.addComponent(btnNewButton)
							.addGap(65)
							.addComponent(btnNewButton_1)
							.addGap(77)
							.addComponent(btnNewButton_2)))
					.addContainerGap(49, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(32)
					.addComponent(lblNewLabel)
					.addGap(76)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(userId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(36)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(UserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(32)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 124, Short.MAX_VALUE)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
						.addComponent(btnNewButton_2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
							.addComponent(btnNewButton_1, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)))
					.addGap(39))
		);
		contentPanel.setLayout(gl_contentPanel);
		this.setLocationRelativeTo(null);
	}

	protected void UserAddActionPerformed(ActionEvent e) {
		String userId = this.userId.getText();
		String userName = this.UserName.getText();
		String password = this.password.getText();
		
		if(StringUtil.isEmpty(userId)) {
			JOptionPane.showMessageDialog(null, "用户账号不能为空！");
			return;
		}
		if(StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空！");
			return;
		}
		if(StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空！");
			return;
		}
		User user = new User(userId, userName, password);
		try {
			int i = systemAdminDao.addUser(user);
			if(i>0) {
				JOptionPane.showMessageDialog(null, "添加用户"+user.getUserName()+"成功！");
				dispose();
				userManagerFrame.fillTable("","");
			}
			else {
				JOptionPane.showMessageDialog(null, "添加用户失败！");
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}

	protected void reset() {
		this.userId.setText("");
		this.UserName.setText("");
		this.password.setText("");
		
	}
}
